/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package demo.model.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author hongnhung
 */
public class StringUtil {
    
    public static String convertToUTF8(String a) throws UnsupportedEncodingException {
        if (a != null && !a.equals("")) {

            byte[] b = a.getBytes("ISO-8859-1");
            String c = new String(b, "UTF-8");
            return c;

        }
        return a;
    }
    
   public static boolean validatePositiveNumber(String number) {
        String temp = "0123456789";
        int i;
        for (i = 0; i < number.length(); i++) {
            int j;
            for (j = 0; j < temp.length(); j++) {
                if (temp.charAt(j) == number.charAt(i)) {
                    j = temp.length();
                }
            }

            if (j != temp.length() + 1) {
                i = number.length();
            }
        }
        if (i == number.length() + 1) {
            return false;
        }
        return true;
    }
   

   public static boolean validateEmail(String email){
        Pattern  pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        Matcher  matcher = pattern.matcher(email);
	return matcher.matches();
   }
   
   public static Integer parseInt(String value){
       if(value != null && !value.equals("")
               && value.charAt(0) == '-' 
               && validatePositiveNumber(value.substring(1, value.length()))){
           return Integer.parseInt(value);
       }else if(value != null && !value.equals("") && validatePositiveNumber(value)){
           return Integer.parseInt(value);
       }
       return null;
   }
   
   public static Integer parseId(String value){
       Integer id = parseInt(value);
       if(id == null){
           return 0;
       }
       return id;
   }
   
   public static Integer parsePage(String value){
       Integer id = parseInt(value);
       if(id == null){
           return 1;
       }
       return id;
   }
   
   
       public static String trim(String text) {
        if (text != null) {
            return text.trim();
        }
        return text;
    }

    public static String convertNullToEmpty(String text) {
        if (text == null) {
            return "";
        }
        return text;
    }


    public static String convertToISO(String a) throws UnsupportedEncodingException {
        if (a != null && !a.equals("")) {
            byte[] b = a.getBytes("UTF-8");
            String c = new String(b, "ISO-8859-1");
            return c;
        }
        return a;
    }


    public static String subString(String str, int index, int indexEnd) {
        return str.substring(index, indexEnd);
    }

    public static String splitWord(String str, int length) {
        if (str != null && !str.equals("")) {
            str = StringUtil.trim(str);
            str = StringUtil.trimLeft(str);
            int index = 0;
            int i = 0;
            while (index < length) {
                while (i < str.length() && str.charAt(i) != ' ') {
                    i++;
                }
                index++;
                while (i < str.length() && str.charAt(i) == ' ') {
                    i++;
                }

            }
            str = StringUtil.subString(str, 0, i);
        }
        return str;
    }

    public static String trimLeft(String str) {
        if (str != null && !str.equals("")) {
            int i = 0;
            while (i < str.length() && str.charAt(i) == ' ') {
                i++;
            }
            str = StringUtil.subString(str, i, str.length());
        }
        return str;
    }

    public static String removeHTML(String str) {
        return str.replaceAll("\\<.*?>", "");
    }

    public static int calTotalPage(int totalRecord, int totalRecoredShow) {
        int totalPage;
        if (totalRecord % totalRecoredShow != 0) {
            totalPage = totalRecord / totalRecoredShow + 1;
        } else {
            totalPage = totalRecord / totalRecoredShow;
        }
        return totalPage;
    }
    
    public static int calIndexOfPage(int page,int recordTotal){
        int indexStart = 0;
        if (page > 1) {
            indexStart = ((page - 1) * recordTotal) + 1;
        }
        if(indexStart==0){
            return indexStart;
        }
        return indexStart-1;
    }
    
    public static String replace(String main,String replaceWord,String keyWord){
        return main.replaceAll(replaceWord, keyWord);
    }
    
    
    
    
     public static String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            // Now we need to zero pad it if you actually want the full 32 chars.
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
     
     public static Boolean isContain(String item, String[] list){
         if(list==null){
             return false;
         }
         
         for(int i=0;i<list.length;i++){
             if(item.equals(list[i])){
                return true;
             }
         }
         return false;
     }
     
     
     public static Boolean isContainer(String item, ArrayList list){
         if(list==null){
             return false;
         }
         
         for(int i=0;i<list.size();i++){
             if(item.equals(list.get(i))){
                return true;
             }
         }
         return false;
     }
     
}
